package com.liangzhong.proxy.goldpalace
{
	import com.adobe.serialization.json.JSON;
	import com.liangzhong.commandconst.commandType.MainCommandType;
	import com.liangzhong.commandconst.goldpalace.CMD_GoldPalace;
	import com.liangzhong.commandconst.logic.CMD_Logic;
	import com.liangzhong.constants.HttpRequestConst;
	import com.liangzhong.errortip.ErrorTips;
	import com.liangzhong.global.CMD_Head;
	import com.liangzhong.model.AppData;
	import com.liangzhong.proxy.BaseProxy;
	import com.liangzhong.proxy.ProxyUtils;
	import com.liangzhong.proxyconstant.goldpalace.GoldPalaceConst;
	import com.socket.clientsocket.IClientSocket;
	
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	
	import lz.controls.Alert;

	/**
	 * 黄金12宫 
	 * @author lake
	 * 
	 */	
	public class GoldPalaceProxy extends BaseProxy
	{
		/**
		 *proxy名字 
		 */
		public static const ProxyName:String = "GoldPalaceProxy";
		
		public function GoldPalaceProxy(proxyName:String=ProxyName, data:Object=null)
		{
			super(proxyName, data);
		}
		/**
		 * 数据读取
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 */		
		override public function onSocketRead(mainCmdID:uint,
											  subCmdID:uint,
											  buffer:ByteArray, 
											  dataSize:int, 
											  clientSocket:IClientSocket):Boolean
		{
			switch(mainCmdID)
			{
				case CMD_Logic.MAIN://主
				{
					return onDeal(mainCmdID,subCmdID,buffer,dataSize);
				}
				case MainCommandType.TASK:
				{
					return onDealTask(mainCmdID,subCmdID,buffer,dataSize);
				}
			}
			return false;
		}
		/**
		 * 处理信息
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @return 
		 */		
		private function onDeal(mainCmdID:uint,
								subCmdID:uint,
								buffer:ByteArray, 
								dataSize:int):Boolean
		{
			var retcode 	:int;
			var activePos	:int;//激活位置
			var pos		:int;//奖励的位置 1 - 12；
			switch(subCmdID)
			{
				case CMD_GoldPalace.ACTIVE_PLAYER://点亮球员
				{
					retcode = buffer.readInt();
					if(retcode == 0)
					{
						activePos = buffer.readByte();
						AppData.getInstance().goldPalaceManager.goldPalaceVo.active ++;
						AppData.getInstance().goldPalaceManager.goldPalaceVo.awardPos = AppData.getInstance().goldPalaceManager.goldPalaceVo.active;
						AppData.getInstance().goldPalaceManager.goldPalaceVo.actives[activePos - 2] = 1;
						AppData.getInstance().goldPalaceManager.goldPalaceVo.begintime = 0;
						if(AppData.getInstance().goldPalaceManager.goldPalaceVo.active == 12)
						{
							//Alert.showErrorTip("开启金色强化");
						}
						var starId :int = AppData.getInstance().goldPalaceManager.goldPalaceVo.starIds[activePos - 2];
						var unStarIds :Array = AppData.getInstance().goldPalaceManager.goldPalaceVo.unStarIds;
						unStarIds.splice(unStarIds.indexOf(starId),1);
					}
					else
					{
						Alert.showErrorTip(ErrorTips.instance.getCErrorTip(retcode) + "");
					}
					this.sendNotification(GoldPalaceConst.ACTIVE_PLAYER_RES,{retcode:retcode,active:activePos});
					return true;
				}
				case CMD_GoldPalace.CHANGE_AWARD://奖励更换
				{
					retcode = buffer.readInt();
					if(retcode == 0)
					{
						pos = buffer.readByte();
						AppData.getInstance().goldPalaceManager.goldPalaceVo.awardPos = pos;
						AppData.getInstance().goldPalaceManager.goldPalaceVo.begintime = AppData.getInstance().time;
					}
					else
					{
						Alert.showErrorTip(ErrorTips.instance.getCErrorTip(retcode) + "");
					}
					this.sendNotification(GoldPalaceConst.CHANGE_AWARD_RES,{retcode:retcode,pos:pos});
					return true;
				}
				case CMD_GoldPalace.DROP_TIME://点券去除CD时间
				{
					retcode = buffer.readInt();
					if(retcode == 0)
					{
						AppData.getInstance().goldPalaceManager.goldPalaceVo.begintime = 0;
					}
					else
					{
						Alert.showErrorTip(ErrorTips.instance.getCErrorTip(retcode) + "");
					}
					this.sendNotification(GoldPalaceConst.DROP_TIME_RES,{retcode:retcode});
					return true;
				}
			}
			return false;
		}
		/**
		 * 处理信息
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @return 
		 */		
		private function onDealTask(mainCmdID:uint,
								subCmdID:uint,
								buffer:ByteArray, 
								dataSize:int):Boolean
		{
			var retcode :int;
			switch(subCmdID)
			{
				case CMD_GoldPalace.GET_AWARD://领取奖励
				{
					retcode = buffer.readInt();
					if(retcode == 0)
					{
						Alert.showErrorTip("领取成功");
						AppData.getInstance().goldPalaceManager.goldPalaceVo.begintime = AppData.getInstance().time;
					}
					else
					{
						Alert.showErrorTip(ErrorTips.instance.getCErrorTip(retcode) + "");
					}
					this.sendNotification(GoldPalaceConst.GET_AWARD_RES,{retcode:retcode});
					return true;
				}
			}
			return false;
		}
		/**
		 *获取信息 
		 * @param param
		 * 
		 */		
		public function getInfo(param:Object):void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.GOLDPALACE_GET_INFO;
			ProxyUtils.instance.webClient(getInfoRes,variables,false);
		}
		/**
		 * 获取信息   result
		 * @param data
		 */		
		private function getInfoRes(data :Object):void
		{
			var str:String = String(data);
			var resultObj :Object;
			try
			{
				resultObj = JSON.decode(str)[0];
			}
			catch(e :Error)
			{
				trace("getInfoRes:",e);
				trace("data:",str);
				return;
			}
			if(resultObj == null)
			{
				trace("",str);
				return;
			}
			if(resultObj.retcode != 0)
			{
				return;
			}
			AppData.getInstance().goldPalaceManager.goldPalaceVo.parse(resultObj.ecliptic);
			this.sendNotification(GoldPalaceConst.GET_INFO_RES);
		}
		/**
		 * 激活球员 
		 * @param param
		 * 
		 */		
		public function activePlayer(param :Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 3,CMD_Logic.MAIN,CMD_GoldPalace.ACTIVE_PLAYER,0);
			sendBuf.writeShort(param.pid);
			sendBuf.writeByte(param.pos);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 * 奖励更换 
		 * @param param
		 * 
		 */		
		public function changeAward(param :Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 1,CMD_Logic.MAIN,CMD_GoldPalace.CHANGE_AWARD,0);
			sendBuf.writeByte(param.pos);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 * 领取奖励
		 * @param param
		 * 
		 */		
		public function getAward(param :Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size,MainCommandType.TASK,CMD_GoldPalace.GET_AWARD,0);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 * 点券去除CD时间 
		 * @param param
		 * 
		 */		
		public function dropTime(param :Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size,CMD_Logic.MAIN,CMD_GoldPalace.DROP_TIME,0);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
	}
}